﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using deviceManage.Models;
using deviceManage.vo;
using deviceManage.Filters;
using QRCoder;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace deviceManage.Controllers
{
    /// <summary>
    /// 设备二维码管理
    /// </summary>
    [CheckLoginAttribute]
    public class BarCodeController : Controller
    {
        private readonly DeviceCtx _context;

        public BarCodeController(DeviceCtx context)
        {
            _context = context;
        } 


        /// <summary>
        /// 下载设备二维码
        /// </summary>
        public void DeviceBarCode(String id)
        {
            Device device = _context.Device.Single(p => p.Id == id);
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(device.Name+"\n"+device.DeviceId+"\n"+device.No, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(20);

            Response.ContentType = "image/jpeg";
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);

            Response.Body.WriteAsync(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
            Response.Body.Close();
        }


    }
}
